package com.wzq.shiro.shirodemo.config;

import com.wzq.shiro.shirodemo.realm.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @author wzq
 * @date 2020/5/26
 * @DESC
 */
@Configuration
public class ShiroConfig {

    @Bean
    public UserRealm getUserRealm(){
        return new UserRealm();
    }
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(getUserRealm());
        return securityManager;
    }

    @Bean( name="shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(getDefaultWebSecurityManager());
        // 添加过滤器
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        // anon --匿名访问
        map.put("/auth/accredit","anon");
        map.put("/auth/registered","anon");
        // authc --认证之后访问
        map.put("/auth/profile","authc");
        factoryBean.setFilterChainDefinitionMap(map);
        factoryBean.setLoginUrl("loginOut");
        return factoryBean;
    }
}
